Imports System.Xml.Serialization
Imports system.io
Imports System.Runtime.Serialization
Public Class ItemDB
    Private ItemXmlFilename As String = "ItemDB.xml"
    Private ArmoryDataPath As String = "..\..\ArmoryData"

    Private Const UnknownName As String = "Unknown"

    Private Shared isUpdated As Boolean = False

    Public Structure Item
        <XmlAttribute()> Dim ID As Long
        <XmlAttribute()> Dim Name As String
        <XmlAttribute()> Dim Qlty As Integer
        <XmlAttribute()> Dim Lvl As Integer
        <XmlAttribute()> Dim Type As String
        '<XmlAttribute()> Dim Drop As String
        '<XmlAttribute()> Dim AreaD As String
        '<XmlAttribute()> Dim Vendor As String
        '<XmlAttribute()> Dim AreaV As String
        '<XmlAttribute()> Dim Quest As String
        '<XmlAttribute()> Dim AreaQ As String
        '<XmlAttribute()> Dim Create As String
    End Structure

    Public Shared Items() As Item = Nothing

    Public Sub New()
        If Items Is Nothing Then
            ReDim Items(0)
            Items(0) = New Item
            Items(0).ID = 0
            Items(0).Name = "none"
            Items(0).Qlty = 0
            Items(0).Lvl = 0
            Items(0).Type = "none"

            Dim filename As String
            filename = My.Computer.FileSystem.CombinePath(My.Computer.FileSystem.CombinePath(My.Computer.FileSystem.SpecialDirectories.AllUsersApplicationData, ArmoryDataPath), ItemXmlFilename)

            Dim XU As New XmlUtils.AccessXml
            Try
                XU.ImportFromXml(Items, filename)
            Catch ex As Exception
            End Try

            Dim i As Long
            Dim ct As Long = 0
            For i = 0 To Items.Length - 1
                If Items(i).Name = UnknownName Then
                    GetItemInfo(Items(i).ID)
                    If Items(i).Name <> UnknownName Then
                        isUpdated = True
                        ct += 1
                    End If
                End If
                If (ct Mod 100) = 0 Then
                    SaveItemDB()
                End If
            Next
        End If

        SaveItemDB()

    End Sub

    Protected Overrides Sub Finalize()
        SortItemDB()
        SaveItemDB()
        MyBase.Finalize()
    End Sub

    Private Sub SaveItemDB()
        If isUpdated Then
            Dim filename As String
            Dim XU As New XmlUtils.AccessXml

            Try
                filename = My.Computer.FileSystem.CombinePath(My.Computer.FileSystem.CombinePath(My.Computer.FileSystem.SpecialDirectories.AllUsersApplicationData, ArmoryDataPath), ItemXmlFilename)
                XU.ExportByXml(Items, filename)
                isUpdated = False
            Catch ex As Exception
            End Try
        End If
    End Sub

    Private Sub SortItemDB()
        If Items Is Nothing Then Exit Sub
        If Items.Length < 2 Then Exit Sub

        Dim compare As New CompareItemDB
        Array.Sort(Items, compare)
    End Sub

    Public Function GetItemInfo(ByVal ItemID As Long) As Item
        Dim ct As Long = 0
        Dim Update As Boolean = False
        If Items Is Nothing Then
            ct = 0
        ElseIf Items.Length = 0 Then
            ct = 0
        Else
            Dim i As Long
            For i = Items.Length - 1 To 0 Step -1
                If Items(i).ID = ItemID Then
                    If Items(i).Name = UnknownName Then
                        ct = i
                        Update = True
                        Exit For
                    Else
                        Return Items(i)
                    End If
                End If
            Next
        End If

        If Not Update Then
            ct = Items.Length
            ReDim Preserve Items(ct)
            Items(ct) = New Item
        End If

        Dim ItemXml As New Xml.XmlDocument
        Dim ItemNode As Xml.XmlElement

        Dim Armory As New AccessArmory
        ItemNode = Armory.GetArmoryItemInfo(ItemID, ItemXml)

        If ItemNode Is Nothing Then
            Items(ct).ID = ItemID
            Items(ct).Name = UnknownName
            Items(ct).Qlty = 0
            Items(ct).Lvl = 0
            Items(ct).Type = UnknownName
            Return Items(ct)
        End If

        If ItemNode.Attributes("name") Is Nothing Then
            Items(ct).ID = ItemID
            Items(ct).Name = UnknownName
            Items(ct).Qlty = 0
            Items(ct).Lvl = 0
            Items(ct).Type = UnknownName
            Return Items(ct)
        End If

        Items(ct).ID = ItemID
        Items(ct).Name = ItemNode.Attributes("name").Value
        Items(ct).Qlty = ItemNode.Attributes("quality").Value
        Items(ct).Lvl = ItemNode.Attributes("level").Value
        Items(ct).Type = ItemNode.Attributes("type").Value

        ''--- drop item
        'Try
        '    Items(ct).Drop = ItemNode.SelectNodes("dropCreatures/creature").Item(0).Attributes("name").Value
        '    Items(ct).AreaD = ItemNode.SelectNodes("dropCreatures/creature").Item(0).Attributes("area").Value
        '    If ItemNode.SelectNodes("dropCreatures/creature").Item(0).Attributes("heroic").Value = "1" Then
        '        Items(ct).AreaD &= " (H)"
        '    End If
        'Catch ex As Exception
        'End Try

        ''--- vendor
        'Try
        '    Items(ct).Vendor = ItemNode.SelectNodes("vendors/creature").Item(0).Attributes("name").Value
        '    Items(ct).AreaV = ItemNode.SelectNodes("vendors/creature").Item(0).Attributes("area").Value
        '    If ItemNode.SelectNodes("vendors/creature").Item(0).Attributes("title").Value <> "" Then
        '        Items(ct).Vendor &= "/" & ItemNode.SelectNodes("vendors/creature").Item(0).Attributes("title").Value
        '    End If
        'Catch ex As Exception
        'End Try

        ''--- quest
        'Try
        '    Items(ct).Quest = ItemNode.SelectNodes("rewardFromQuests/quest").Item(0).Attributes("name").Value
        '    Items(ct).AreaQ = ItemNode.SelectNodes("rewardFromQuests/quest").Item(0).Attributes("area").Value
        '    If ItemNode.SelectNodes("vendors/creature").Item(0).Attributes("type").Value <> "" Then
        '        Items(ct).AreaQ &= " (" & ItemNode.SelectNodes("vendors/creature").Item(0).Attributes("type").Value & ")"
        '    End If
        'Catch ex As Exception
        'End Try

        ''--- create
        'Try
        '    Items(ct).Create = ItemNode.SelectNodes("createdBy/spell").Item(0).Attributes("icon").Value
        'Catch ex As Exception
        'End Try

        isUpdated = True

        Return Items(ct)
    End Function

End Class


Public Class CompareItemDB
    Implements IComparer

    Public Function Compare(ByVal x As Object, ByVal y As Object) As Integer Implements System.Collections.IComparer.Compare
        Dim d1 As ItemDB.Item = CType(x, ItemDB.Item)
        Dim d2 As ItemDB.Item = CType(y, ItemDB.Item)

        If d1.ID < d2.ID Then
            Return -1
        ElseIf d1.ID > d2.ID Then
            Return 1
        End If

        Return 0
    End Function
End Class


